home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / util / time / DCF77.lha / DCF77 / Developer / WaitDCF77ifCrash.c < prev    next >
C/C++ Source or Header  |  1996-07-31  |  3KB  |  115 lines

  1. /* WaitDCF77ifCrash                                                 ***RGR***
  2.  
  3.    Author:   Ralf Gruner, Großschönau, Germany.
  4.  
  5.              ralf.gruner@t-online.de
  6. */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/semaphores.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12. #include <stdio.h>
  13.  
  14. BPTR    DateFile =NULL;
  15. char *filename1    ="ENVARC:LastBootDate";
  16. BPTR    CONWin =NULL;
  17. char *filename2    ="CON:20/20/280/80/WaitDCF77ifCrash/AUTO";
  18.  
  19. char *version="$VER: WaitDCF77ifCrash 1.03 (4.1.96)";
  20.  
  21. struct StatusList
  22. {
  23.     struct SignalSemaphore sl_Semaphore;
  24.     STRPTR version;
  25.     BOOL Received;
  26. };
  27.  
  28. struct StatusList *DCF77PublicStatus=NULL;
  29.  
  30. UBYTE    * DCF77SemaphoreName = "DCF77 State";
  31. BOOL    DCF77received=FALSE;
  32.  
  33. main(int argc, char *argv[])
  34. {
  35.     struct DateStamp DS, DSfromFile;
  36.     long bytes;
  37.     char *msg1="";
  38.     char *msg2=    " © 1994-1996 Ralf Gruner, Großschönau, Germany.\n\n"
  39.                     " WaitDCF77ifCrash waits for received time from DCF77\n if the date of your hardware clock has been corrupted.\n"
  40.                     " Usage: Place it in a script above your date sensitive application.\n\n";
  41.  
  42.     if(argc != 1)
  43.     {
  44.         puts(msg1);
  45.         puts(version+5);
  46.         puts(msg2);
  47.     }
  48.     else
  49.     {
  50.         DateStamp((struct DateStamp *)&DS);    // Systemzeit in Struktur eintragen
  51.         
  52.         if (NULL!=(DateFile = Open(filename1,MODE_OLDFILE)))
  53.         {
  54.             bytes = Read(DateFile,&DSfromFile,(LONG)sizeof(struct DateStamp));
  55.             Close(DateFile);
  56.     
  57.             if (bytes != (LONG)sizeof(struct DateStamp))
  58.             {
  59.                 DSfromFile.ds_Days=0;
  60.             }
  61.     
  62.         }
  63.         else
  64.             DSfromFile.ds_Days=0;
  65.  
  66.         if(DSfromFile.ds_Days > DS.ds_Days || DSfromFile.ds_Days < DS.ds_Days-182)
  67.         {
  68.             Delay(500L);    // ggf. auf Workbench-Window warten
  69.             if (NULL!=(CONWin = Open(filename2,MODE_NEWFILE)))
  70.                 FPrintf(CONWin,"\n\n Waiting for DCF77...\n\n");
  71.  
  72.             while(!DCF77received)
  73.             {
  74.                 Forbid();
  75.                 if(DCF77PublicStatus=(struct StatusList *) FindSemaphore(DCF77SemaphoreName))
  76.                 {
  77.                     ObtainSemaphoreShared((struct SignalSemaphore *) DCF77PublicStatus);
  78.                     DCF77received=DCF77PublicStatus->Received;
  79.                     ReleaseSemaphore((struct SignalSemaphore *) DCF77PublicStatus);
  80.                 }
  81.                 else
  82.                     DCF77received=FALSE;    // DCF77 läuft nicht
  83.                 Permit();
  84.     
  85.                 if(!DCF77received)
  86.                     Delay(100L);    // zwei Sekunden warten und dann erneut testen
  87.             }
  88.  
  89.             DateStamp((struct DateStamp *)&DS);    // neue Systemzeit in Struktur eintragen
  90.  
  91.             if(CONWin)
  92.             {
  93.                 FPrintf(CONWin," O.K.\n");
  94.                 Delay(150L);
  95.                 Close(CONWin);
  96.             }
  97.         }
  98.  
  99.         if(DSfromFile.ds_Days != DS.ds_Days)
  100.         {
  101.             // nur speichern, wenn Tag geändert
  102.             if (NULL!=(DateFile = Open(filename1,MODE_NEWFILE)))
  103.             {
  104.                 bytes = Write(DateFile,&DS,(LONG)sizeof(struct DateStamp));
  105.                 if (bytes != (LONG)sizeof(struct DateStamp))
  106.                 {
  107.                     puts("Error while writing");
  108.                 }
  109.                 Close(DateFile);
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115.